<?php
namespace backend\controllers;

use backend\models\ClueOrgin;
use backend\models\CompanyCats;
use backend\models\Departments;
use backend\models\EnterpriseGrade;
use backend\models\FollowType;
use backend\models\Region;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\Admin;
use backend\models\Clue;
use backend\models\Enterprises;
use backend\models\CoordinationDegree;
use backend\models\FollowInfos;

/**
 * Clue controller
 */
class EnterprisesController extends Controller
{
    public $enableCsrfValidation = false;//开启防止crf攻击
    //public $layout='base';
    /**
     * @inheritdoc
     */

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


    /**
     * 企业的首页面
     */
    public function actionIndex()
    {
        $provinceList=Region::getProvinceList();
        $citiesList=Region::getCitiesList();
        $companyCatsList=CompanyCats::getCompanyCatsList();
        $gradeList=EnterpriseGrade::getEnterpriseGrade();
        $followStatusList=FollowType::getFollowTypeList(2);
        $departmentList=Departments::getAllDeparmentList();
        $enterpriseQuery=Enterprises::find()->joinWith('province')->joinWith('city')->joinWith('contacts')->joinWith('companyType')->joinWith('enterpriseGrade')->joinWith('followStatus')->joinWith('users');
        $page=new Pagination(['totalCount'=>$enterpriseQuery->count()]);
        $page->setPageSize(2);
        $enterpriseList=$enterpriseQuery->offset($page->offset)->limit($page->limit)->asArray()->all();

        $data=array();
        $data['provinceList']=$provinceList;
        $data['citiesList']=$citiesList;
        $data['gradeList']=$gradeList;
        $data['followStatusList']=$followStatusList;
        $data['departmentList']=$departmentList;
        $data['companyCatsList']=$companyCatsList;
        $data['enterpriseList']=$enterpriseList;
        $data['page']=$page;


        return $this->renderPartial('index',$data);
    }

    /**
     * 添加企业
     */
    public function actionEditEnterprised(){
        $params=Yii::$app->request->post();
        $id=0;
        $enterpriseName='';
        $checkEnterpriseAccount='';
        $checkEnterpriseName=Enterprises::findOne(['enterpriseName'=>$enterpriseName]);
        $checkEnterpriseAccount=Enterprises::findOne(['enterpriseAccount'=>$checkEnterpriseAccount]);
        if(!empty($checkEnterpriseAccount)||!empty($checkEnterpriseName)){
            returnResult(100024,'企业名称或企业账号已经存在');
        }
        if($id>0){
            $enterprises=Enterprises::findOne(['id'=>$id]);
            if(empty($enterprises)){
                returnResult(100025,'企业记录不存在');
            }
        }else{
            $enterprises=new Enterprises();
        }

        $enterprises->enterpriseName=getData($params['enterpriseName'],'请填写企业名称');
        $enterprises->enterpriseAccount=getData($params['enterpriseAccount'],'填写企业账号');
        $enterprises->legalPerson=getData($params['legalPerson'],'请填写法人');
        $enterprises->responName=getData($params['responName'],'请填写企业负责人');
        $enterprises->provinceId=getData($params['provinceId'],'请选择省份');
        $enterprises->cityId=getData($params['cityId'],'请选择城市');
        $enterprises->address=getData($params['address'],'请填写具体地址');
        $enterprises->companyType=getData($params['companyType'],'请填写具体地址');
        $enterprises->contactName=getData($params['contactName'],'请填写联系人');
        $enterprises->phone=getData($params['phone'],'请填写联系电话');
        $enterprises->idCardStatus=getData($params['idCardStatus'],'请选择授权信息');
        $enterprises->isSame=getData($params['isSame'],'请选择信息是否一致');
        $enterprises->nextFollowTime=getData($params['nextFollowTime'],'请选择下次跟进时间');
       // $enterprises->responRoleId=nextFollowTime
        $enterprises->responUserId=getData($params['responUserId'],'请选择负责客服');
        $enterprises->applyTime=getData($params['applyTime'],'请填写申请时间');
        $enterprises->recommender=getData($params['recommender'],'请填写推荐人');
        $enterprises->recomCell=getData($params['recomCell'],'请填写推荐人手机号码');
        $enterprises->registerBonus=getData($params['registerBonus'],'请填写注册金额');
        $enterprises->enterprisePhone=getData($params['enterprisePhone'],'请填写企业电话');
        $enterprises->fax=$params['fax'];
        $enterprises->email=$params['email'];
        $enterprises->enterpriseSite=$params['enterpriseSite'];
        $enterprises->mainProduct=getData($params['mainProducts'],'请填写主营产品');
        $enterprises->standardId=getData($params['standardId'],'请选择产品标准');
        $enterprises->stock=getData($params['stock'],'请填写库存');
        $enterprises->monthSale=getData($params['monthSale'],'请填写月销售量');
        $enterprises->processMsg=getData($params['processMsg'],'加工处理情况');
        $enterprises->mainAddress=$params['mainAddress'];
        $enterprises->remarks=$params['remarks'];;
        $doResult=$enterprises->save();
        if($doResult){
            returnResult(0,'操作成功');
        }else{
            returnResult(100026,'操作失败');
        }


    }
    /**
     * 企业详情
     */
    public function actionEnterpriseDetail(){
        $id=Yii::$app->request->get('id');
        $coordinationList=CoordinationDegree::getCoordinationDegreeList();
        $followTypeList=FollowType::getFollowTypeList(3);
        $followStatusList=FollowType::getFollowTypeList(2);
        $enterpriseDetail=Enterprises::find()->joinWith('province')->joinWith('city')->joinWith('contacts')->joinWith('companyType')->joinWith('enterpriseGrade')->joinWith('followStatus')->joinWith('users')->joinWith('productStandard')->where(['al_enterprises.id'=>$id])->asArray()->one();

        $followInfosList=FollowInfos::find()->joinWith('followType')->joinWith('followStatus')->joinWith('coordinationDegree')->joinWith('users')->where(['al_follow_infos.enterpriseId'=>$id,'al_follow_infos.type'=>2])->orderBy('id DESC')->asArray()->all();

        $data=['enterpriseDetail'=>$enterpriseDetail,'followInfosList'=>$followInfosList];
        $data['coordinationList']=$coordinationList;
        $data['followTypeList']=$followTypeList;
        $data['followStatusList']=$followStatusList;
        $data['departmentsList']=Departments::getAllDeparmentList();
        return $this->renderPartial('enterprise-detail',$data);
    }
     /**
     * 指派企业的负责人
     */
    public function actionAssignEnter(){
        $params=Yii::$app->request->post();
        $enterpriseId=getData($params['enterpriseId'],'记录不存在');
        $responUserId=getData($params['responUserId'],'请选择负责人');
        $checkEnterprise=Enterprises::findOne(['id'=>$enterpriseId]);
        if(empty($checkEnterprise)){
            returnResult(100043,'找不到该记录');
        }
        $checkEnterprise->responUserId=$responUserId;
        $doResult=$checkEnterprise->save();
        if($doResult){
            returnResult(0,'指派成功');
        }else{
            returnResult(100068,'指派失败');
        }
    }

      /**
     * 删除企业
     */
    public function actionDelEnterprise(){
        $params=Yii::$app->request->post();
        $enterprise=Enterprises::findOne(['id'=>$params['enterpriseId']]);
        if(empty($enterprise)){
            returnResult(100040,'无法删除该记录');
        }
        $doResult=$enterprise->delete();
        if($doResult){
            returnResult(0,'删除成功');
        }else{
            returnResult(100041,'删除失败');
        }
    }







}
